import axios from "@/tools/axios-config";
import { auth } from "@/tools/auth";


export const getExercisesByCourseId = (courseId) => {
    return axios.get(`/exercises/?course=${courseId}`).then((res) => res.data);
};

export const getQuestionsByExerciseId = (exerciseId) => {
    return axios.get(`/questions/?exercise=${exerciseId}`).then((res) => res.data);
};

export const updateExerciseNameById = ({ newExerciseName, exerciseId, courseId }) => {
    return axios({
        method: "patch",
        url: `/exercises/${exerciseId}/`,
        data: JSON.stringify({
            name: newExerciseName,
            course: courseId
        }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8',
            ...(auth.currentUser && {
                'Authorization': `Token ${auth.currentUser.token}`
            }),
        },
    }).then((res) => (res.data))
};

export const createExerciseByCourseId = ({ exerciseName, courseId }) => {

    return axios({
        method: "post",
        url: `/exercises/`,
        data: JSON.stringify({
            name: exerciseName,
            course: courseId,
        }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8',
            ...(auth.currentUser && {
                'Authorization': `Token ${auth.currentUser.token}`
            }),
        },
    }).then((res) => (res.data))
}

export const updateQuestionById = ({ newContent, newAnswer, newCategory, newScore, questionId, courseId }) => {
    return axios({
        method: "patch",
        url: `/questions/${questionId}/`,
        data: JSON.stringify({
            content: newContent,
            answer: newAnswer,
            category: newCategory,
            score: newScore,
            course: courseId,
        }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8',
            ...(auth.currentUser && {
                'Authorization': `Token ${auth.currentUser.token}`
            }),
        },
    }).then((res) => (res.data))
}

export const createQuestionByExerciseId = ({ newContent, newAnswer, newCategory, newScore, exerciseId, courseId }) => {

    return axios({
        method: "post",
        url: `/questions/`,
        data: JSON.stringify({
            content: newContent,
            answer: newAnswer,
            category: newCategory,
            score: newScore,
            exercise: exerciseId,
            course: courseId
        }),
        headers: {
            'Content-type': 'application/json; charset=UTF-8',
            ...(auth.currentUser && {
                'Authorization': `Token ${auth.currentUser.token}`
            }),
        },
    }).then((res) => (res.data))
}